<!DOCTYPE html>
<link rel=author href="mailto:jarhar@chromium.org">
<link rel=help href="https://github.com/openui/open-ui/issues/599">
<link rel=help href="https://bugs.chromium.org/p/chromium/issues/detail?id=1359089">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>

<selectlist id=defaultlistbox>
  <option>one</option>
  <option>two</option>
</selectlist>

<selectlist id=customlistbox>
  <listbox>
    <option>one</option>
    <option>two</option>
  </listbox>
</selectlist>

<script>
const tabKey = '\uE004';

document.querySelectorAll('selectlist').forEach(selectlist => {
  promise_test(async () => {
    selectlist.focus();
    await test_driver.send_keys(selectlist, ' ');
    assert_true(selectlist.open, 'Listbox should be open after pressing space.');

    selectlist.addEventListener('keydown', event => {
      if (event.key === 'Tab') {
        event.preventDefault();
      }
    }, {once: true});
    await test_driver.send_keys(document.activeElement, tabKey);
    assert_true(selectlist.open, 'Listbox should stay open when the tab keydown is preventDefaulted.');

    await test_driver.send_keys(document.activeElement, tabKey);
    assert_false(selectlist.open, 'Listbox should close after pressing the tab key.');
  }, `${selectlist.id}: Pressing tab should close the listbox.`);
});
</script>
